package mongo.db.clients.firsts;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoClientOpt {

	/**
	 * @param args
	 * @throws InterruptedException
	 */

	public static void main(String[] args) throws InterruptedException {

		List<ServerAddress> seeds = null;
		try {
			seeds = new ArrayList<ServerAddress>();
			seeds.add(new ServerAddress("202.191.136.234", 27017));
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		MongoClient client = new MongoClient(seeds);
		DB db;
		DBCollection collection;

		ExecutorService connectionExeService = Executors.newFixedThreadPool(150);
		List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();

		for (int j = 0; j < 3; j++) {

			for (int i = 0; i < 900; i++) {
				MongoExecutorService execService = new MongoExecutorService(client);
				futureList.add(connectionExeService.submit(execService));
			}
			System.out.println("futureList size : " + futureList.size());
			try {
				for (Future<Integer> future : futureList) {
					future.get();
				}
			} catch (Exception e) {
				System.out.println("MapReduceThread : TopNThread " + e);
			}

			System.out.println("iterating for : " + j);
			Thread.sleep(10000);
		}
		futureList.clear();
		connectionExeService.shutdownNow();

		System.out.println("futureList completed ");
		Thread.sleep(60000);
	}

}
